package com.random.fox.whatspay.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.View;

/**
 * 主页带动画view
 * Created by 渠 on 2014/9/1.
 */
public class LoadingView extends View {
    private String TAG = getClass().getSimpleName();
    private Path p=new Path();
    private final int w;
    private final int h;
    private int y;
    private final float time = 1000;

    public LoadingView(Context context) {
        super(context);
        Display mDisplay = ((Activity)context).getWindowManager().getDefaultDisplay();
        w = mDisplay.getWidth();
        h = mDisplay.getHeight();
        setBackgroundColor(Color.parseColor("#282222"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        p.reset();
        p.moveTo(0,0);
        p.lineTo(0, h - w / 2 + y);
        p.lineTo(w/4,h-w/4+ y);
        p.lineTo(w,h-w+ y);
        p.lineTo(w,0);
        p.close();
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#964611"));
        canvas.drawPath(p,paint);

    }

    public void move(int y){
        this.y = y;
        Log.d(TAG,y+"");
        Handler handler = getHandler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                invalidate();
            }
        });
    }

    public void reset(){
        new Thread(){
            int tip = (int) (y*10/time);
            int last = (int) (time/10);

            @Override
            public void run() {
                super.run();
                while (last-->0){
                    try {
                        Thread.sleep(10);
                        y=y-tip;
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                Handler handler = getHandler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        invalidate();
                    }
                });
            }
        }.start();
    }
}
